import { request } from '../../utils/request'
import type { UserInfo, ApiResponse } from '../../types/app.d'

/**
 * 获取用户列表
 */
export const getUserList = (params: {
  page: number
  pageSize: number
  username?: string
  nickname?: string
  status?: number
  deptId?: string
  beginTime?: string
  endTime?: string
}) => {
  return request.get<ApiResponse<{
    list: UserInfo[]
    total: number
  }>>('/system/user/list', { params })
}

/**
 * 获取用户详情
 */
export const getUserInfo = (id: string) => {
  return request.get<ApiResponse<UserInfo>>(`/system/user/${id}`)
}

/**
 * 创建用户
 */
export const createUser = (data: Omit<UserInfo, 'id' | 'createTime' | 'updateTime'>) => {
  return request.post<ApiResponse>('/system/user', data)
}

/**
 * 更新用户
 */
export const updateUser = (id: string, data: Partial<Omit<UserInfo, 'id' | 'createTime'>>) => {
  return request.put<ApiResponse>(`/system/user/${id}`, data)
}

/**
 * 删除用户
 */
export const deleteUser = (id: string) => {
  return request.delete<ApiResponse>(`/system/user/${id}`)
}

/**
 * 批量删除用户
 */
export const batchDeleteUser = (ids: string[]) => {
  return request.delete<ApiResponse>('/system/user/batch', { data: { ids } })
}

/**
 * 修改用户状态
 */
export const changeUserStatus = (id: string, status: number) => {
  return request.put<ApiResponse>(`/system/user/${id}/status`, { status })
}

/**
 * 重置用户密码
 */
export const resetUserPassword = (id: string, password: string) => {
  return request.put<ApiResponse>(`/system/user/${id}/resetPassword`, { password })
}

/**
 * 更新用户角色
 */
export const updateUserRoles = (id: string, roleIds: string[]) => {
  return request.put<ApiResponse>(`/system/user/${id}/roles`, { roleIds })
}

/**
 * 更新用户部门
 */
export const updateUserDept = (id: string, deptId: string) => {
  return request.put<ApiResponse>(`/system/user/${id}/dept`, { deptId })
}

/**
 * 更新用户岗位
 */
export const updateUserPost = (id: string, postId: string) => {
  return request.put<ApiResponse>(`/system/user/${id}/post`, { postId })
}